package com.zlh123.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.zlh123.entity.Classes;
import com.zlh123.entity.Student;
import com.zlh123.mapper.ClassesMapper;
import com.zlh123.mapper.StudentMapper;
import com.zlh123.service.StudentService;
import com.zlh123.util.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
@Service
public class StudentServiceImpl implements StudentService {
    @Autowired
    private StudentMapper studentMapper;
    @Autowired
    private ClassesMapper classesMapper;

    @Override
    public List<Student> findAll() {
        List<Student> students = studentMapper.selectByExample(null);
        students.forEach(st -> {
            Classes classes = classesMapper.selectByPrimaryKey(st.getCid());
            st.setCname(classes.getCname());
        });
        return students;
    }
    @Override
    public PageResult<Student> findBypage(int page, int pageSize) {
        //分页
        PageHelper.startPage(page, pageSize);
        //查询数据
        List<Student> students = this.findAll();

        Page<Student> studentPage = (Page<Student>) students;
        return new PageResult<>(studentPage.getResult(), studentPage.getTotal());
    }
}
